package com.hee.erp.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hee.erp.modules.entity.CarArchives;
import com.hee.erp.modules.entity.CarBuyingFiles;
import com.hee.erp.modules.entity.SysUser;
import com.hee.erp.modules.mapper.CarArchivesMapper;
import com.hee.erp.modules.service.ICarArchivesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hee.erp.modules.service.ICarBuyingFilesService;
import com.hee.erp.modules.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author weizehong
 * @since 2022-05-02
 */
@Service
@Transactional
public class CarArchivesServiceImpl extends ServiceImpl<CarArchivesMapper, CarArchives> implements ICarArchivesService {

    @Autowired
    ISysUserService sysUserService;


    @Autowired
    ICarBuyingFilesService carBuyingFilesService;

    @Override
    public CarArchives getByCarId(String id) {
        //根据carId查询车辆档案
        LambdaQueryWrapper<CarArchives> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CarArchives::getCarId, id);

        CarArchives one = getOne(queryWrapper);
        if(one != null){
            setForeignKey(one);

            //购车资料
            List<CarBuyingFiles> buyingFilesList = carBuyingFilesService.getListByCarId(id);
            one.setCarBuyingFilesList(buyingFilesList);

            return one;
        }
        return new CarArchives();
    }

    private void setForeignKey(CarArchives entity){
        //设置managerName
        if(entity.getManagerId() != null){
            SysUser user = sysUserService.getById(entity.getManagerId());
            if(user != null){
                entity.setManagerName(user.getUserName());
            }
        }
    }
}
